/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifndef __CONNMAN_RTNL_H
#define __CONNMAN_RTNL_H

#ifdef __cplusplus
extern "C" {
#endif

#include <linux/rtnetlink.h>

/**
 * SECTION:rtnl
 * @title: RTNL premitives
 * @short_description: Functions for registering RTNL modules
 */

#define CONNMAN_RTNL_PRIORITY_LOW      -100
#define CONNMAN_RTNL_PRIORITY_DEFAULT     0
#define CONNMAN_RTNL_PRIORITY_HIGH      100

#define	CONNMAN_RTNL_DEVICE_ANY		-1

struct connman_rtnl {
	const char *name;
	int priority;
	int index;		/* device index to match or -1 for any device */
	void *private;

	void (*newlink)(void *, int index, unsigned short type,
	    const char *ifname, unsigned flags, int change);
	void (*dellink)(void *, int index, unsigned short type,
	    const char *ifname, unsigned flags, int change);
	void (*newaddr)(void *, int index, const char *label, int family,
	    int prefixlen, const char *addr);
	void (*deladdr)(void *, int index, const char *label, int family,
	    int prefixlen, const char *addr);
	void (*newroute)(void *, int index, int scope, const char *dst,
	    const char *gateway);
	void (*delroute)(void *, int index, int scope, const char *dst,
	    const char *gateway);
};

/* for static declarations where index and private aren't meaningful */
#define	RTNL_DEFINE(_name, _pri) \
	.name = _name, \
	.priority = _pri, \
	.index = CONNMAN_RTNL_DEVICE_ANY, \
	.private = NULL

/* for runtime initialization/setup */
#define	RTNL_INIT(_rtnl, _name, _pri, _index, _priv) do { \
	(_rtnl)->name = (_name); \
	(_rtnl)->priority = (_pri); \
	(_rtnl)->index = (_index); \
	(_rtnl)->private = (_priv); \
} while (0)

int connman_rtnl_register(struct connman_rtnl *rtnl);
void connman_rtnl_unregister(struct connman_rtnl *rtnl);

struct connman_rtnl_reader {
	int fd;
	int address_size;
	int len;
	unsigned char *ptr;
	unsigned char buf[4096];
};

struct connman_rtnl_reader *connman_rtnl_open_route(unsigned char family,
                                                    unsigned int flags,
                                                    void *dst_addr,
                                                    uint32_t index);
connman_bool_t connman_rtnl_read_route(struct connman_rtnl_reader *reader,
                                       uint32_t *index,
                                       unsigned char *dst_len,
                                       unsigned char *src_len,
                                       void *dst, void *src, void *gateway,
                                       unsigned int *flags);
void connman_rtnl_close(struct connman_rtnl_reader *reader);

#ifdef __cplusplus
}
#endif

#endif /* __CONNMAN_RTNL_H */
